﻿using Beryl.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace Beryl.OrderControllers
{
    public class CuttingRec
    {
        private static CuttingRec instance = null;
        private static readonly object uniqueLock = new object();
        public static CuttingRec Instance
        {
            get
            {
                lock (uniqueLock)
                {
                    if (instance == null)
                    {
                        instance = new CuttingRec();
                    }
                }
                return instance;
            }
        }
        public CuttingRec() { }

        public List<string> CuttingDataQuery(string para)
        {
            List<string> list = new List<string>();
            try
            {
                var sql = string.Format(@"select * from View_CutReceive where cOrderNo = '{0}' order by dPlanQty desc,dWidth desc;",para);
                string strPicYZ = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql));
                if (strPicYZ.Equals("[]"))
                {
                    throw new Exception("数据为空");
                }

                list.Add(strPicYZ);
            }
            catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }

        //数据提交
        public List<string> UpdateData(string para)
        {
            List<string> list = new List<string>();
            Newtonsoft.Json.Linq.JArray jsonArr = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(para);
            try
            {
                var k = jsonArr[0];
                string wh = k["wh"].ToString();
                string data = k["data"].ToString();
                Newtonsoft.Json.Linq.JArray jsonArr1 = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(data);
                var updateSql = "";
                var insertSql1 = "INSERT INTO WMS_Receive(cMatNo, Category, createTime, cMOLotNo, cBatch, fQuantity, cSpecifucation, dProductDate, dInputDate, cProductDepNo, warehousecode, cSupplier, inum, irate, cPlace) VALUES ";
                var insertSql2 = "INSERT INTO WMS_Retrieval(cMatNo, Category, createTime, cOrderNo, cMOLotNo, fQuantity, dProductDate, dOutputDate, warehousecode, cSupplier, cProductDepNo, inum, irate) VALUES ";
                for (int i = 0; i < jsonArr1.Count; i++)
                {
                    var resData = jsonArr1[i];
                    var cMatNo = resData["cMatNo"].ToString(); //已切料编码
                    var cMatNo1 = resData["cMatNo1"].ToString(); //未切料编码
                    var dLength = double.Parse(resData["dLength"].ToString()); //长度
                    var dWidth = double.Parse(resData["dWidth"].ToString()); //宽度
                    var rkWeight = double.Parse(resData["rkWeight"].ToString()); //入库重量
                    var dPlanQty = double.Parse(resData["dPlanQty"].ToString()); //片数
                    var ddz = double.Parse(resData["ddz"].ToString()); //单重
                    var dReqQty = double.Parse(resData["dReqQty"].ToString());//计划数
                    var cateNo = resData["cCateNo"].ToString();
                    var cateNo1 = cMatNo1.Substring(0, 5); //未切
                    double fQuantity = 0; //已切主计量
                    double inum = 0; //已切副计量
                    double irate = 0; //已切换算率
                    double fQuantity1 = 0; //未切主计量
                    double inum1 = 0; //未切副计量
                    double irate1 = 0; //未切换算率

                    if (cateNo == "Y0104" || cateNo == "Y0192" || cateNo == "Y0105")
                    { //已切正负箔
                        fQuantity = Math.Round((ddz * rkWeight / dWidth * 1000), 2);  //单位米数
                        inum = Math.Round(rkWeight, 2); //单位kg
                        irate = Math.Round((1 / ddz), 6);
                    }

                    if (cateNo == "Y0106") //已切电解纸
                    {
                        fQuantity = Math.Round(rkWeight, 2); //单位kg
                        inum = Math.Round((dWidth * dPlanQty * dLength / 1000), 2); //单位平方米
                    }


                    if (cateNo1 == "Y0101" || cateNo1 == "Y0191" || cateNo1 == "Y0102")
                    { //未切正负箔
                        fQuantity1 = Math.Round(ddz * rkWeight, 2); //单位平方米
                        inum1 = Math.Round(rkWeight, 2); //单位kg
                        irate1 = Math.Round((1 / ddz), 6);
                    }

                    if (cateNo1 == "Y0103") //未切电解纸
                    {
                        fQuantity1 = Math.Round(rkWeight, 2); //单位kg
                        inum1 = Math.Round(ddz * rkWeight, 2); //单位平方米
                    }
                    var cBatchNumber = resData["cBatchNumber"].ToString() == "" ? "" : resData["cBatchNumber"].ToString(); //未切料批次号
                    var cMOLotNo = resData["cMOLotNo"].ToString(); //生产批次号
                    var cOrderNo = resData["cOrderNo"].ToString(); //生产订单号
                    var cSpecifution = resData["cSpecifution"].ToString() == "" ? "" : resData["cSpecifution"].ToString(); //已切料规格
                    var dProductDate = resData["dProductDate"].ToString(); //已切料生产日期
                    var times = CommonMethod.Instance.getTimes();
                    var dInputDate = times;
                    var dOutputDate = times;
                    var cSupplierInfo = resData["cSupplierInfo"].ToString() == "" ? "" : resData["cSupplierInfo"].ToString(); //未切料供应商名称
                    var cPlace = resData["cPosCode"].ToString() == "" ? "" : resData["cPosCode"].ToString(); //入库货位
                    var cProductDepNo = resData["cProductDepNo"].ToString();
                    var warehousecode = wh; //入库仓库
                    var warehousecode1 = "21-1"; //出库仓库

                    if (i < jsonArr1.Count - 1)
                    {
                        //已切料写入入库台账WMS_Receive
                        insertSql1 += string.Format(@"('{0}', 3, '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}',{12},'{13}'), ", cMatNo, times, cMOLotNo, cOrderNo, fQuantity,
                            cSpecifution, dProductDate, dInputDate, cProductDepNo, warehousecode, cSupplierInfo, inum, irate, cPlace);
                        //未切料写入出库台账WMS_Retrieval
                        insertSql2 += string.Format(@"('{0}',3,'{1}','2','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}',{11}), ", cMatNo1, times, cMOLotNo, cBatchNumber, fQuantity1, dProductDate, dOutputDate,
                            warehousecode1, cSupplierInfo, cProductDepNo, inum1, irate1);
                    }
                    else
                    {
                        //已切料写入入库台账WMS_Receive
                        insertSql1 += string.Format(@"('{0}', 3, '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}',{12},'{13}'); ", cMatNo, times, cMOLotNo, cOrderNo, fQuantity,
                            cSpecifution, dProductDate, dInputDate, cProductDepNo, warehousecode, cSupplierInfo, inum, irate, cPlace);
                        //未切料写入出库台账WMS_Retrieval
                        insertSql2 += string.Format(@"('{0}',3,'{1}','2','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}',{11}); ", cMatNo1, times, cMOLotNo, cBatchNumber, fQuantity1, dProductDate, dOutputDate,
                            warehousecode1, cSupplierInfo, cProductDepNo, inum1, irate1);
                    }
                    updateSql += string.Format(@"update APS_ValidBOM set cCutFlag = '0' where cOrderType =  '3' and cOrderNo = '{0}' and cSpecifution = '{1}' and cBatchNumber = '{2}';", cOrderNo, cSpecifution, cBatchNumber);
                }

                var connstr = SqlHelper.GetConnSting();
                SqlHelper.ExecuteNonQuery(connstr, CommandType.Text, insertSql1 + insertSql2);
                list.Add("success");
            }
            catch(Exception e)
            {
                list.Add(e.Message);
            }
            return list;
        }
    }
}